package com.ruiysoft.service.order.facade;

import com.ruiysoft.controller.order.vo.OrderReceiverInfoVo;
import com.ruiysoft.entity.common.UserInfo;
import com.ruiysoft.entity.order.OrderDetail;
import com.ruiysoft.entity.order.OrderMain;
import com.ruiysoft.entity.order.OrderPay;
import com.ruiysoft.entity.order.OrderReceiverInfo;
import com.ruiysoft.entity.order.OrderSkuInfo;
import com.ruiysoft.entity.order.OrderSkuPic;
import com.ruiysoft.service.order.bean.OrderPlaceSkuInfoVo;
import com.ruiysoft.service.order.consts.DistributionMode;
import com.ruiysoft.service.order.consts.OrderBizTypeEnum;
import com.ruiysoft.service.order.consts.OrderDBStatusEnum;
import com.ruiysoft.service.order.consts.OrderDistributionSettlementStatusEnum;
import com.ruiysoft.service.order.consts.OrderPayStatusEnum;
import com.ruiysoft.service.order.consts.OrderTypeEnum;
import com.ruiysoft.service.order.consts.PayWayEnum;
import com.ruiysoft.util.CodeGenerator;
import com.ruiysoft.util.NumberConst;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Date : 2018/11/10
 *
 * @author WenJialu
 */
@Component
public class OrderAssembleFacade {

	@Resource
	OrderCalculateFacade oCalcFacade;


	/**
	 * 组装订单主表数据
	 */
	public OrderMain assembleOrderMain(UserInfo usr, OrderTypeEnum orderType, OrderBizTypeEnum orderBizType, OrderMain orderMain) {
		/*Long totalAmount = oCalcFacade.calculateOrderTotalAmount(orderPlaceSkuInfoVos);
		Long freight = oCalcFacade.calculateFreight(totalAmount, usr.getShopId());
		Long receivableAmount = oCalcFacade.add(totalAmount, freight);*/
		return orderMain
				.setOrderNo(CodeGenerator.INSTANCE.genOrderNoCustomerPlace())
				.setShopId(usr.getShopId())
				.setUserId(usr.getPersonnelId())
				.setOpenId(usr.getOpenid())
				.setType(orderType.getCode())
				.setBizType(orderBizType.getCode())
				.setStatus(OrderDBStatusEnum.CUSTOMER_CONFIRMED_WAITING_FOR_PAYMENT.getCode())
				.setDistributionStatus(OrderDistributionSettlementStatusEnum.NON_SETTLE.getCode())
				.setCreateTime(new Date())
				.setCancelTime(null)
				.setPayTime(null)
				.setPayType(PayWayEnum.WECHAT_PAY.getCode())//暂时只支持微信
				.setCreator(usr.getPersonnelId())
				//.setTotalAmount(totalAmount)
				//.setReceivableAmount(receivableAmount)
				.setCollectedAmount(NumberConst.ZERO_LONG)
				.setDistributionMode(DistributionMode.EXPRESS.getCode())//暂时默认自取
				//.setFreight(freight)
				.setDelFlag(false);
	}

	public OrderPay assembleOrderPay(OrderMain orderMain) {
		return new OrderPay()
				.setOrderId(orderMain.getId())
				.setOrderNo(orderMain.getOrderNo())
				.setOrderAmount(orderMain.getReceivableAmount())
				.setTransactionNo(null)
				.setAmount(NumberConst.ZERO_LONG)
				.setPayType(PayWayEnum.WECHAT_PAY.getCode())
				.setStatus(OrderPayStatusEnum.WAITING_FOR_PAY.getCode())
				.setPayTime(null)
				.setUpdateTime(null)
				.setMessage(null);
	}

	public List<OrderDetail> assembleOrderDetails(OrderMain orderMain, List<OrderPlaceSkuInfoVo> orderPlaceSkuInfoVos) {
		return orderPlaceSkuInfoVos
				       .stream()
				       .map(vo -> new OrderDetail()
						       .setOrderId(orderMain.getId())
						       .setOrderNo(orderMain.getOrderNo())
						       .setSkuId(vo.getSkuId())
							   .setPriceStrategy(vo.getPriceStrategy())
						       .setUnitPrice(vo.getUnitPrice())
							   .setCostPrice(vo.getCostPrice())
							   .setVirtualCoinPrice(vo.getVirtualCoinPrice())
						       .setQuantity(vo.getQuantity())
							   .setTotalAmount(vo.getTotalAmount())
							   .setTotalCash(vo.getTotalCash())
							   .setVirtualCoin(vo.getVirtualCoin()))
				.collect(Collectors.toList());
	}

	public List<OrderSkuInfo> assembleOrderSkuInfos(OrderMain orderMain, List<OrderPlaceSkuInfoVo> orderPlaceSkuInfoVos) {
		return orderPlaceSkuInfoVos
				       .stream()
				       .map(vo -> new OrderSkuInfo()
						       .setOrderId(orderMain.getId())
						       .setOrderNo(orderMain.getOrderNo())
						       .setSkuId(vo.getSkuId())
						       .setSpuId(vo.getSpuId())
						       .setSkuModel(null)
						       .setMerchandiseName(vo.getSkuName())
							   .setPriceStrategy(vo.getPriceStrategy())
						       .setListPrice(vo.getListPrice())
						       .setCostPrice(vo.getCostPrice())
						       .setUnitPrice(vo.getUnitPrice())
							   .setVirtualCoinPrice(vo.getVirtualCoinPrice())
						       .setProfitMargin(null)
						       .setMainCategory(null)
						       .setShopId(vo.getShopId())
						       .setFreightTemplateId(null)
						       .setCreator(orderMain.getCreator()))
				.collect(Collectors.toList());
	}

	public List<OrderSkuPic> assembleOrderSkuPic(OrderMain orderMain, List<OrderPlaceSkuInfoVo> orderPlaceSkuInfoVos) {
		return orderPlaceSkuInfoVos
				       .stream()
				       .map(vo -> new OrderSkuPic()
						       .setOrderId(orderMain.getId())
						       .setOrderNo(orderMain.getOrderNo())
						       .setSkuId(vo.getSkuId())
						       .setPath(vo.getImgUrl())
						       .setSort(NumberConst.ZERO_INT))
				.collect(Collectors.toList());
	}

	public OrderReceiverInfo assembleOrderReceiverInfo(OrderMain orderMain, @Valid OrderReceiverInfoVo orderReceiverInfoVo) {
		OrderReceiverInfo orderReceiverInfo = new OrderReceiverInfo()
				.setOrderId(orderMain.getId())
				.setOrderNo(orderMain.getOrderNo())
				.setUserId(orderMain.getUserId())
				.setUserAddressId(null)
				.setReceiverName(orderReceiverInfoVo.getUserName())
				.setReceiverPhone(orderReceiverInfoVo.getTelNumber())
				.setReceiverAddress(orderReceiverInfoVo.getDetailInfo())
				.setProvince(orderReceiverInfoVo.getProvinceName())
				.setProvinceCode(null)
				.setCity(orderReceiverInfoVo.getCityName())
				.setCityCode(null)
				.setDistrict(orderReceiverInfoVo.getCountyName())
				.setDistriceCode(null)
				.setArea(null)
				.setAreaCode(null)
				.setCreateTime(new Date());

		orderReceiverInfo.setPostalCode(orderReceiverInfoVo.getPostalCode());

		return orderReceiverInfo;
	}
}
